package cn.com.css.taxInspection.gt3.service.impl;

import cn.com.css.taxInspection.gt3.entity.ZxhjQqtjBzEntity;
import cn.com.css.taxInspection.gt3.entity.ZxhjQqtjEntity;
import cn.com.css.taxInspection.gt3.mapper.ZxhjQqtjBzMapper;
import cn.com.css.taxInspection.gt3.mapper.ZxhjQqtjMapper;
import cn.com.css.taxInspection.gt3.service.ZxhjQqtjService;
import cn.com.css.taxInspection.util.DateUtils;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ZxhjQqtjService")
public class ZxhjQqtjServiceImpl extends ServiceImpl<ZxhjQqtjMapper, ZxhjQqtjEntity> implements ZxhjQqtjService {

    @Resource
    private ZxhjQqtjMapper zxhjQqtjMapper;

    @Resource
    private ZxhjQqtjBzMapper zxhjQqtjBzMapper;
    @Override
    public List<ZxhjQqtjEntity> queryQqtjTable(ZxhjQqtjEntity zxhjQqtjEntity) {
        List<ZxhjQqtjEntity> zxhjQqtjEntityList = new ArrayList<>();
        ZxhjQqtjEntity error = new ZxhjQqtjEntity();

        // 判断是否传入了特定月份
        String specifiedMonth = TsBqUtils.isNotNull(zxhjQqtjEntity.getYf()) ? zxhjQqtjEntity.getYf() : DateUtils.getCurrentMonth();

        // 1. 确定查询基准月份
        YearMonth baseMonth;
        if (specifiedMonth != null && !specifiedMonth.isEmpty()) {
            // 使用传入的月份作为基准
            baseMonth = YearMonth.parse(specifiedMonth, DateTimeFormatter.ofPattern("yyyyMM"));
        } else {
            // 使用当前月份作为基准
            baseMonth = YearMonth.now();
        }

        String baseMonthStr = baseMonth.format(DateTimeFormatter.ofPattern("yyyyMM"));
        int year = baseMonth.getYear();
        String swjgmc = zxhjQqtjEntity.getSwjgmc();
        if (swjgmc == null ||swjgmc.equals("")) {
            extracted("沈阳市税务稽查局", baseMonthStr, zxhjQqtjEntityList, year);
            extracted("沈阳市税务局第一稽查局", baseMonthStr, zxhjQqtjEntityList, year);
            extracted("沈阳市税务局第二稽查局", baseMonthStr, zxhjQqtjEntityList, year);
            extracted("沈阳市税务局第三稽查局", baseMonthStr, zxhjQqtjEntityList, year);
        }else {
            extracted(swjgmc, baseMonthStr, zxhjQqtjEntityList, year);
        }

        return zxhjQqtjEntityList;
    }

    @Override
    public Boolean updateQqtjbz(ZxhjQqtjEntity zxhjQqtjEntity) {
        String swjgmc = zxhjQqtjEntity.getSwjgmc();
        String yf = zxhjQqtjEntity.getYf();
        String bz = zxhjQqtjEntity.getBz();
        Integer nf = Integer.valueOf(zxhjQqtjEntity.getYf().substring(0, 4));
        String lc = zxhjQqtjEntity.getXh();
        Map<String, Object> selectMap = new HashMap<>();
        selectMap.put("swjgmc",swjgmc);
        selectMap.put("yf",yf);
        selectMap.put("nf",nf);
        selectMap.put("lc",lc);
        List<ZxhjQqtjBzEntity> zxhjQqtjBzEntities = zxhjQqtjBzMapper.selectByMap(selectMap);
        ZxhjQqtjBzEntity zxhjQqtjBzEntity = new ZxhjQqtjBzEntity();
        zxhjQqtjBzEntity.setSwjgmc(swjgmc);
        zxhjQqtjBzEntity.setYf(yf);
        zxhjQqtjBzEntity.setBz(bz);
        zxhjQqtjBzEntity.setNf(nf);
        zxhjQqtjBzEntity.setLc(lc);
        if (zxhjQqtjBzEntities == null ) {
            int insert = zxhjQqtjBzMapper.insert(zxhjQqtjBzEntity);
            if (insert==1){
                return true;
            }else {
                return false;
            }
        }
       else {
            zxhjQqtjBzMapper.updateByUnionKey(zxhjQqtjBzEntity);
            return true;
        }
    }

    private void extracted(String swjgmc, String baseMonthStr, List<ZxhjQqtjEntity> zxhjQqtjEntityList, int year) {
        // 4. 查询上月累计数据
        ZxhjQqtjEntity lastMonthData = zxhjQqtjMapper.sumLastMt(baseMonthStr, swjgmc);
        if (TsBqUtils.isNotNull(lastMonthData)) {
            lastMonthData.setXh("上月累计");
            zxhjQqtjEntityList.add(lastMonthData);
        }else {
            ZxhjQqtjEntity zxhjQqtjEntity = new ZxhjQqtjEntity();
            zxhjQqtjEntity.setXh("上月累计");
            zxhjQqtjEntity.setSwjgmc(swjgmc);
            zxhjQqtjEntity.setBz("数据全部为0");
            zxhjQqtjEntityList.add(zxhjQqtjEntity);
        }
        // 3. 查询当前月份数据
        ZxhjQqtjEntity currentMonthData = zxhjQqtjMapper.findByYfAndSwjg(baseMonthStr, swjgmc);
        if (TsBqUtils.isNotNull(currentMonthData)) {
            currentMonthData.setXh("本月发生");
            zxhjQqtjEntityList.add(currentMonthData);
        }else {
            ZxhjQqtjEntity zxhjQqtjEntity = new ZxhjQqtjEntity();
            zxhjQqtjEntity.setXh("本月发生");
            zxhjQqtjEntity.setSwjgmc(swjgmc);
            zxhjQqtjEntity.setBz("数据全部为0");

            zxhjQqtjEntityList.add(zxhjQqtjEntity);
        }

        // 4. 查询本年累计数据
        ZxhjQqtjEntity yearData = zxhjQqtjMapper.sumByYearBySwjg(baseMonthStr,year, swjgmc);
        if (TsBqUtils.isNotNull(yearData)) {
            yearData.setXh("本年累计");
            zxhjQqtjEntityList.add(yearData);
        }else {
            ZxhjQqtjEntity zxhjQqtjEntity = new ZxhjQqtjEntity();
            zxhjQqtjEntity.setXh("本年累计");
            zxhjQqtjEntity.setSwjgmc(swjgmc);
            zxhjQqtjEntity.setBz("数据全部为0");
            zxhjQqtjEntityList.add(zxhjQqtjEntity);
        }
    }


}